package com.mtnz.njk.Service;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.mtnz.njk.Mapper.CouponManagerMapper;
import com.mtnz.njk.Mapper.CouponProductMapper;
import com.mtnz.njk.Mapper.CouponTypeMapper;
import com.mtnz.njk.Mapper.ProductMapper;
import com.mtnz.njk.entity.CouponProductEntity;
import com.mtnz.njk.entity.CouponTypeEntity;
import com.mtnz.njk.entity.ProductEntity;
import com.mtnz.njk.result.PageInfo;
import com.mtnz.njk.result.ResponseMessage;
import com.mtnz.njk.result.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 代金券管理服务层
 * @Author: FBL
 * @CreateDate: 2019\8\23 0023 18:12
 * @Version: 1.0
 */
@Service
public class CouponManagerService {

    @Autowired
    CouponTypeMapper couponTypeMapper;

    @Autowired
    CouponManagerMapper couponManagerMapper;

    @Autowired
    CouponProductMapper couponProductMapper;

    @Autowired
    ProductMapper productMapper;

    /**
     * 代金券列表
     *
     * @param key
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    public ResponseResult listCouponType(String key, Integer pageNum, Integer pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        // 查询代金券类型列表
        List<CouponTypeEntity> couponTypeEntities = couponManagerMapper.listCouponType(key);
        PageInfo<CouponTypeEntity> couponTypeEntityPageInfo = new PageInfo<>(couponTypeEntities);
        List<CouponTypeEntity> list = couponTypeEntityPageInfo.getList();
        for (CouponTypeEntity couponTypeEntity : list) {
            ArrayList<ProductEntity> productEntities = new ArrayList<>();
            // 添加代金券商品信息
            Integer couponTypeId = couponTypeEntity.getCouponTypeId();
            Example couponProductExample = new Example(CouponProductEntity.class);
            couponProductExample.createCriteria().andEqualTo("couponTypeId", couponTypeId);
            List<CouponProductEntity> couponProductEntities = couponProductMapper.selectByExample(couponProductExample);
            for (CouponProductEntity couponProductEntity : couponProductEntities) {
                ProductEntity productEntity = productMapper.selectByPrimaryKey(couponProductEntity.getProductId());
                productEntities.add(productEntity);
            }
            // 商品信息置入代金券中
            couponTypeEntity.setProductEntities(productEntities);
        }
        return new ResponseResult(ResponseMessage.OK, couponTypeEntityPageInfo);
    }

    /**
     * 编辑代金券
     *
     * @param couponTypeEntity
     * @return
     * @throws Exception
     */
    @Transactional
    public ResponseResult updateCoupon(CouponTypeEntity couponTypeEntity) throws Exception {
        // 更新代金券基本信息
        couponTypeMapper.updateByPrimaryKeySelective(couponTypeEntity);
        // 更新代金券商品信息
        if (couponTypeEntity.getProductEntities() != null && couponTypeEntity.getProductEntities().size() > 0) {
            List<ProductEntity> productEntities = couponTypeEntity.getProductEntities();
            if(couponTypeEntity.getOldProductIds() != null && couponTypeEntity.getOldProductIds().size() > 0){
                // 删除原来的商品信息
                List<String> oldProductIds = couponTypeEntity.getOldProductIds();
                Example couponProductExample = new Example(CouponProductEntity.class);
                couponProductExample.createCriteria().andIn("productId", oldProductIds);
                couponProductMapper.deleteByExample(couponProductExample);
            }
            productEntities.forEach(s -> {
                String productId = s.getProductId();
                // 新增现有的商品信息
                CouponProductEntity couponProductEntity = new CouponProductEntity();
                couponProductEntity.setCouponTypeId(couponTypeEntity.getCouponTypeId());
                couponProductEntity.setProductId(productId);
                couponProductEntity.setCreateTime(DateUtil.now());
                couponProductMapper.insertSelective(couponProductEntity);
            });
        }
        return new ResponseResult(ResponseMessage.OK);
    }

    /**
     * 新增代金券
     *
     * @param couponTypeEntity
     * @return
     * @throws Exception
     */
    @Transactional
    public ResponseResult insertCouponType(CouponTypeEntity couponTypeEntity) throws Exception {
        // 新增代金券基本信息
        couponTypeEntity.setCreateTime(new Date());
        int i = couponTypeMapper.insertSelective(couponTypeEntity);
        // 查询最近插入代金券的id
        Integer couponTypeId = couponManagerMapper.getCouponTypeId();
        // 新增代金券商品信息
        if(couponTypeEntity.getProductEntities() != null && couponTypeEntity.getProductEntities().size() > 0){
            List<ProductEntity> productEntities = couponTypeEntity.getProductEntities();
            for (ProductEntity productEntity : productEntities) {
                // 新增现有的商品信息
                CouponProductEntity couponProductEntity = new CouponProductEntity();
                couponProductEntity.setCouponTypeId(couponTypeId);
                couponProductEntity.setProductId(productEntity.getProductId());
                couponProductEntity.setCreateTime(DateUtil.now());
                couponProductMapper.insertSelective(couponProductEntity);
            }
        }
        if(i > 0)
            return new ResponseResult(ResponseMessage.OK);
        return new ResponseResult(ResponseMessage.FAIL);
    }


}
